package com.bugsnag.ohos;

import java.io.IOException;

/**
 * Serializes and deserializes the device ID to/from JSON.
 *
 * @since 2021-07-05
 */
public class DeviceId implements JsonStream.Streamable {
    private static final String KEY_ID = "id";

    private static JsonReadable<DeviceId> jsonReadable = reader -> {
        String id = null;
        try {
            reader.beginObject();
            if (reader.hasNext() && KEY_ID == reader.nextName()) {
                id = reader.nextString();
            }
        } catch (IOException e) {
            e.getMessage();
        }
        return new DeviceId(id);
    };
    String id;

    /**
     * constructor
     *
     * @param id
     */
    public DeviceId(String id) {
        this.id = id;
    }

    @Override
    public void toStream(JsonStream writer) throws IOException {
        writer.beginObject();
        writer.name(KEY_ID);
        writer.value(id);
        writer.endObject();
    }
}